/**
 * Copyright 北京彩通农旗资讯有限公司 All rights reserved.
 * 北京彩通农旗资讯有限公司 专有/保密源代码,未经许可禁止任何人通过任何* 渠道使用、修改源代码.
 */
package com.xspace.android.xafcommon.db.dbsub;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.xspace.android.xafcommon.been.OfflineMianyiDataBean;
import com.xspace.android.xafcommon.db.BaseSql;
import com.xspace.android.xafcommon.util.LogUtil;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

/**
 * 离线代办任务
 * 
 * <p>
 * Copyright: Copyright (c) 2011
 * </p>
 * <p>
 * 公司名称 : 北京彩通农旗资讯有限公司
 * </p>
 * <p>
 * 项目名称 : MAF-Android
 * </p>
 * <p>
 * 创建时间 : 2016年4月22日 上午10:55:44
 * </p>
 * <p>
 * 类描述 :
 * </p>
 *
 *
 * @version 1.0.0
 * @author <a href=" ">liuchen</a>
 */
public class OfflinePedingItemDB extends BaseSql {
	private final String type = "type ";
	private final String userid = "userid ";
	private final String objid = "objid ";
	private final String isselect = "isselect ";
	private final String content = "content ";
	private final String formid = "formid";
	private final String InsertTime = "inserttime";
	private final String tag = "OfflinePedingItemDB";
	private MyDBHelper helpDb;
	private SQLiteDatabase sqldb;
	private String TableName = "CacheDaiBan";

	@Override
	public void create(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TableName + " (_ID INTEGER PRIMARY KEY AUTOINCREMENT," + type + " text," + userid
				+ "  text," + objid + "  text," + isselect + " text," + content + "  text," + formid + "  text,"
				+ InsertTime + "  text)");
	}

	public OfflinePedingItemDB() {

	}

	public void dropTable(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TableName);
	}

	public OfflinePedingItemDB(MyDBHelper db) {
		super(db);
		helpDb = db;
	}

	@Override
	public void upgrade(SQLiteDatabase arg0) {

	}

	private void openDB() {
		helpDb.open();
		sqldb = helpDb.mDatabase;

	}

	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


	/**
	 * 
	 * 方法描述 : 插入更新待办数据
	 * 
	 * @param type
	 * @param userid
	 * @param objid
	 * @param isselect
	 * @param content
	 * @param formid
	 * @param insertTime
	 */
	public void insertOrUpdate(String type, String userid, String objid, String isselect, String content, String formid,
			long insertTime) {
		if (TextUtils.isEmpty(content))
			return;
		String insertTimeStr = df.format(new Date(insertTime));
		openDB();
		ContentValues cv = new ContentValues();
		String[] columns = { this.type, this.userid, this.objid, this.isselect, this.content };
		String selection = this.type + "=? and " + this.userid + "=? and " + this.objid + "=? and " + this.formid
				+ "=?";
		String[] selectionArgs = new String[] { type, userid, objid, formid };
		Cursor cur = sqldb.query(TableName, columns, selection, selectionArgs, null, null, null);
		if (cur != null && cur.getCount() > 0) {
			cv.put(this.isselect, isselect);
			sqldb.update(TableName, cv, selection, selectionArgs);
			LogUtil.v(tag, "insertOrUpdate update>>>" + objid + " isselect:" + isselect);
		} else {
			cv.put(this.type, type);
			cv.put(this.userid, userid);
			cv.put(this.objid, objid);
			cv.put(this.isselect, isselect);
			cv.put(this.content, content);
			cv.put(this.InsertTime, insertTimeStr);
			cv.put(this.formid, formid);
			long result = sqldb.insert(TableName, null, cv);
			LogUtil.v(tag,
					"insertOrUpdate insert>>>InsertTime:" + insertTimeStr + " " + "type:" + type + " userid:" + userid
							+ " objid:" + objid + "isselect:" + isselect + "content:" + content + "insert result:"
							+ result);
		}
		cur.close();
	}

	/**
	 * 
	 * 方法描述 : 查询所有数据
	 * 
	 * @param userid
	 * @return
	 */
	public List<OfflineMianyiDataBean> queryAllData(String userid) {
		List<OfflineMianyiDataBean> list = new ArrayList<OfflineMianyiDataBean>();
		openDB();
		String selection = this.userid + "=?";
		String[] selectionArgs = new String[] { userid };
		String[] columns = { this.type, this.userid, this.objid, this.isselect, this.content, this.formid };
		Cursor cur = sqldb.query(TableName, columns, selection, selectionArgs, null, null, null);
		if (cur != null && cur.getCount() > 0) {
			while (cur.moveToNext()) {
				String type = cur.getString(0);
				String objid = cur.getString(2);
				String isselect = cur.getString(3);
				String content = cur.getString(4);
				String formid = cur.getString(5);
				LogUtil.v(tag, "queryAllData >>> Objectid:" + objid + " Content:" + content + " >>userid:" + userid);
				OfflineMianyiDataBean logb = new OfflineMianyiDataBean(type, userid, objid, isselect, formid, content);
				list.add(logb);
			}
		}
		cur.close();
		LogUtil.v(tag, "queryAllData count:" + list.size() + " userid:" + userid);
		return list;
	}

	public void DeleteData(String userid) {
		openDB();
		// String[] whereArgs=new String[]{userid};
		int result = sqldb.delete(TableName, null, null);
		LogUtil.v(tag, "DeleteOneData userid:" + userid + " result:" + result);

	}
}
